package com.my.swas.sso.module.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.my.swas.common.base.ResponseResult;
import com.my.swas.common.utils.BeanCopierUtil;
import com.my.swas.sso.module.admin.dto.MenuDto;
import com.my.swas.sso.module.admin.entity.Menu;
import com.my.swas.sso.module.admin.service.MenuService;
import com.my.swas.sso.module.admin.vo.ResTreeSelectVo;
import com.my.swas.sso.module.admin.vo.ZTreeMenuVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @Resourcesor: :MaYong
 */
@Slf4j
@Controller
@Transactional
@RequestMapping("admin/menu")
public class MenuController {
    @Resource
    MenuService menuService;

    /**
     * 获取所有的资源信息
     * @param model
     * @param menuDto
     * @return
     */
    @RequiresPermissions("operate:menu:view")
    @PostMapping(value = "list")
    @ResponseBody
    public IPage getResourcesList(Model model, MenuDto menuDto) {
        Menu menu = new Menu();
        BeanCopierUtil.convert(menuDto,menu);
        IPage pageInfo = menuService.page(menuDto.getPage(), new QueryWrapper<>(menu));
        return pageInfo;
    }

    /**
     * 获取资源树结构,权限配置选择器-> zTree 数据格式
     * @param roleId
     * @return
     */
    @GetMapping(value = "treeList")
    @ResponseBody
    public List<ZTreeMenuVo> zTreeList(Long roleId) {
        List<ZTreeMenuVo> resTreeVoList = menuService.queryResourcesByRoleId(Lists.newArrayList(roleId));
        return resTreeVoList;
    }

    /**
     * 树形选择器-> treeSelect 数据格式
     * @return
     */
    @GetMapping(value = "treeSelectList")
    @ResponseBody
    public List<ResTreeSelectVo> treeSelectList(){
        return menuService.treeSelectList(null,null,null);
    }

    /**
     * 跳转编辑角色页面
     * @param id
     * @return
     */
    @GetMapping("toSavePage")
    public String toSavePage(Long id,Model model){
        Menu Resources = new Menu();
        Resources.setId(id);
        Menu dbResources = menuService.getOne(new QueryWrapper<>(Resources));
        if(null == dbResources){
            dbResources = new Menu();
    }
        model.addAttribute("res",dbResources);
        return "layui/system/menu/add";
    }

    /**
     * 添加角色
     * @param menuDto
     * @return
     */
    @RequiresPermissions("operate:menu:save")
    @PostMapping("save")
    @ResponseBody
    public ResponseResult saveResources(MenuDto menuDto) {
        Menu menu = new Menu();
        BeanCopierUtil.convert(menuDto,menu);
        if(null == menuDto.getId()) {
            menuService.save(menu);
        }else{
            menuService.updateById(menu);
        }
        return ResponseResult.ok;
    }

    /**
     * 删除角色
     * @param ids
     * @return
     */
    @GetMapping("remove")
    @RequiresPermissions("operate:menu:delete")
    @ResponseBody
    public ResponseResult removeResources(String ids){
        menuService.removeByIds(Splitter.on(",").splitToList(ids));
        return ResponseResult.ok;
    }

    /**
     * 跳转编辑页面
     * @param id
     * @return
     */
    @GetMapping("toEditPage")
    public String toEditPage(Long id,Model model){
        Menu Resources = new Menu();
        Resources.setId(id);
        Menu dbResources = menuService.getOne(new QueryWrapper<>(Resources));
        model.addAttribute("res",dbResources);
        return "layui/system/menu/edit";
    }

}
